<template>
  <div>

    <el-form ref="form" :rules="rules" :model="form" label-width="90px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item class="add" label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          class="add"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" class="add" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" class="add" placeholder="请选择聘用形式">
          <el-option v-for="obj in informaltype" :key="obj.id" :label="obj.value" :value="obj.id" />

        </el-select>
      </el-form-item>
      <el-form-item label="部门" class="add" prop="departmentName" @click.native="shuxin=true">
        <el-input v-model="form.departmentName" class="add" placeholder="请选择部门" />
        <el-tree
          v-show="shuxin"
          :data="data"
          :props="{ label:'name' }"
          accordion
          default-expand-all

          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请选择工号" />
      </el-form-item>
      <el-form-item class="add" label="转正时间" prop="correctionTime">

        <el-date-picker
          v-model="form.correctionTime"
          class="add"
          type="date"
          placeholder="选择日期"
        />

      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm,$emit('clode')">取 消</el-button>
      <el-button type="primary" @click="submitFn('form')">确 定</el-button>
    </span>

  </div>
</template>

<script>
import { addEmployessAPI } from '@/api/employeesAPI'
import informaltype from '@/constant/employees'
import attendance from '@/constant/attendance'

import { DepartmentsAPI } from '@/api/departmentsAPI'

import { TreeData } from '@/utils/shuxin'
let total = 0
let ind = 0
export default {
  name: 'AddEmployyes',
  data() {
    return {
      informaltype: informaltype.informaltype,
      shuxin: false,

      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      form: { // 绑定

        username: 'ssss', // 姓名
        mobile: '13333333333', // 手机
        formOfEmployment: '1', // 平庸形式
        workNumber: '123', // 工号
        timeOfEntry: '2001-1-1', // 入职时间
        departmentName: '财务部', // 部门
        correctionTime: '2001-1-1' //  转正时间

      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'change' }
        ]

      }

    }
  },

  created() {
    this.Departments()
  },
  methods: {
    // 新增员工
    submitFn(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const time = setInterval(async() => {
            total++
            ind++
            if (ind > 10) {
              ind = 1
            }
            this.form.username = `这是${attendance.vacationtype[ind].name}`
            if (total === 100) {
              console.log('完成')
              clearInterval(time)
            }
            const res = await addEmployessAPI({
              ...this.form
            })
            console.log(total)
            console.log(res)
            this.$emit('dialogVisible')
          }, 500)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async  Departments() {
      const res = await DepartmentsAPI()
      console.log(res)
      this.data = TreeData(res.data.depts, '')
    },
    handleNodeClick(a, b, c) {
      this.form.departmentName = a.name
      this.shuxin = false
    },
    resetForm() {
      // 1. 把表单的数据项清空
      this.form = {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.add{
   width:510px;
}
.el-form-item__content{
    .add{
   width:580px;
}
}
</style>
